package com.umpay.business.payment.impl;

import java.util.HashMap;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.umpay.business.payment.PaymentBusinessService;
import com.umpay.config.Regex;
import com.umpay.dict.DataDict;
import com.umpay.dict.XmlData;
import com.umpay.dict.retcode.MerAccessEC;
import com.umpay.dto.UMFResponse.Message;
import com.umpay.dto.UMFResponse.Message.Meta;
import com.umpay.dto.merAccess.req.payment.AppPayOrderAccessReq;
import com.umpay.dto.merAccess.req.payment.PaymentCodePayAccessReq;
import com.umpay.dto.merAccess.req.payment.WechatPlatformPayReq;
import com.umpay.dto.merAccess.res.payment.AppPayOrderAccessRes;
import com.umpay.dto.merAccess.res.payment.PaymentCodePayAccessRes;
import com.umpay.dto.merAccess.res.payment.WechatPlatformPayRes;
import com.umpay.dto.merBusi.req.payment.AppPayOrderBusiReq;
import com.umpay.dto.merBusi.req.payment.MPOSBusiReq;
import com.umpay.dto.merBusi.req.payment.PaymentCodePayBusiReq;
import com.umpay.dto.merBusi.req.payment.WechatPublicPlatformReq;
import com.umpay.dto.merBusi.res.payment.AppPayOrderBusiRes;
import com.umpay.dto.merBusi.res.payment.MPOSBusiRes;
import com.umpay.dto.merBusi.res.payment.PaymentCodePayBusiRes;
import com.umpay.dto.merBusi.res.payment.WechatPublicPlatformRes;
import com.umpay.exception.BusinessException;
import com.umpay.exception.RedirectBusiException;
import com.umpay.feign.client.ConsumeBusiServerFeignClient;
import com.umpay.util.HsplusUtil;
import com.umpay.util.PropertiesUtil;
import com.umpay.util.ReqParaCheckUtil;
import com.umpay.util.RpidUtils;
import com.umpay.util.StringUtils;
import com.umpay.util.TimeUtil;

/*******************************************************************
 * <br>
 * description : 支付请求业务模块接口处理类
 * @author cxy
 * @date 2018年7月9日 上午20:42:11
 * @version 1.0
 ******************************************************************/
@Service
public class PaymentBusinessServiceImpl implements PaymentBusinessService {

	public static final Logger log = LogManager.getLogger(PaymentBusinessServiceImpl.class);
	@Autowired
	private ConsumeBusiServerFeignClient consumeBusiServerFeignClient;

	
	@Override
	public Message<WechatPlatformPayRes> wechatPlatformPay(WechatPlatformPayReq req) {

		log.info(String.format("公众号支付，请求参数为:%s", req));
		//参数校验
		String payType = StringUtils.trim(req.getPay_type()) ;                   //	             支付方式
		if(!(payType.equals(DataDict.WECHAT) || payType.equals(DataDict.ALIPAY))) {
			throw new BusinessException(MerAccessEC.REQPARAM_ERR, "公众号支付请求参数[pay_type]格式不正确");
		}
		String merId = StringUtils.trim(req.getMer_id()) ;                       //	            商户号
		//String retUrl = StringUtils.trim(req.getRet_url()) ;                   //		前台通知地址
		String merTrace = StringUtils.trim(req.getMer_trace());                  //	             商户流水号
		String tradeNo = StringUtils.trim(req.getTrade_no()) ;                   //		订单号						
		if(DataDict.WECHAT.equals(payType)) {
			payType = "18";
		}else if(DataDict.ALIPAY.equals(payType)) {
			payType = "19";
		}
		String amount = StringUtils.trim(req.getAmount()) ;                      //		 支付金额
		String expireTime = StringUtils.trim(req.getExpire_time());              //      过期时间
		String openId = StringUtils.trim(req.getOpenid()) ;                      //      用户授权标识
		String appId = StringUtils.trim(req.getAppid()) ;                        //	              微信及支付宝的 appId       
		String version = StringUtils.trim(req.getVersion()) ;  
		String remark = StringUtils.trim(req.getRemark()) ;                      //	           备注信息
		String notifyUrl = StringUtils.trim(req.getNotify_url()) ;               //	          服务器异步通知路径
		String bProductId = PropertiesUtil.getBusiStrValue("bsp.bproductid", "") ; //		业务产品号
		String pProductId="";
		if("18".equals(payType)) {
			pProductId = PropertiesUtil.getBusiStrValue("bsp.officialAccountPay.wechat.pproductid", "") ; //	支付产品号
		}else if("19".equals(payType)) {
			pProductId = PropertiesUtil.getBusiStrValue("bsp.officialAccountPay.alipay.pproductid", "") ; //	支付产品号			
		}
		String goodsInf = StringUtils.trim(req.getGoods_inf());	
		String goodsId = StringUtils.trim(req.getGoods_id());
		//组织请求merBusi参数
		WechatPublicPlatformReq wechatPublicPlatformReq = new WechatPublicPlatformReq();
		wechatPublicPlatformReq.setRpid(RpidUtils.getRpid());//通信流水
		wechatPublicPlatformReq.setReqDate(TimeUtil.date8());//请求日期
		wechatPublicPlatformReq.setReqTime(TimeUtil.time6());//请求时间
		wechatPublicPlatformReq.setMerId(merId); //商户流水号
		wechatPublicPlatformReq.setPayType(payType);//支付类型
		wechatPublicPlatformReq.setMerTrace(merTrace); //	商户流水号
		wechatPublicPlatformReq.setTradeNo(tradeNo); //订单交易号
		wechatPublicPlatformReq.setAmount(amount);//支付金额
		wechatPublicPlatformReq.setExpireTime(expireTime);//过期时间
		wechatPublicPlatformReq.setVersion(version); //	版本号		
		wechatPublicPlatformReq.setOpenId(openId); //用户授权标识
		wechatPublicPlatformReq.setAppId(appId); //	微信及支付宝的 appId 		
		wechatPublicPlatformReq.setbProductId(bProductId); //	业务产品号
		wechatPublicPlatformReq.setpProductId(pProductId); //	支付产品号
		wechatPublicPlatformReq.setNotifyUrl(notifyUrl); //	服务器异步通知路径
		wechatPublicPlatformReq.setRemark(remark); //	备注信息
		wechatPublicPlatformReq.setGoodsInf(goodsInf);
		wechatPublicPlatformReq.setGoodsId(goodsId);
		//请求交易模块merBusi
		log.info(String.format("公众号支付，调用远程服务接口请求参数为:%s", wechatPublicPlatformReq));
		Message<WechatPublicPlatformRes> merBusiRes = this.consumeBusiServerFeignClient.wechatPlatformPay(wechatPublicPlatformReq);
		log.info(String.format("公众号支付，调用远程服务接口获取结果为:%s", merBusiRes));

		// 组织接入模块响应结果
		Message<WechatPlatformPayRes> resMessage = new Message<WechatPlatformPayRes>();
		Meta resMeta = new Meta();
		//判断业务响应结果，不为空
		if (merBusiRes != null) {
			Meta metaData = merBusiRes.getMeta();
			String retCode = StringUtils.trim(metaData.getRet_code());
			String retMsg = StringUtils.trim(metaData.getRet_msg());
			resMeta.setRet_code(retCode);//返回码
			resMeta.setRet_msg(retMsg);//返回描述
			
			if(StringUtils.isEmpty(retCode)) {
				resMeta.setRet_code(MerAccessEC.SYSTEM_ERR);
				resMeta.setRet_msg("系统忙");
				
			}else if(MerAccessEC.SUCCESS.equals(retCode)) {//成功				
				WechatPublicPlatformRes resInfo = merBusiRes.getData();
				WechatPlatformPayRes merAccessRes = new WechatPlatformPayRes();
				merAccessRes.setMer_id(merId);//商户号
				merAccessRes.setVersion(DataDict.VERSION);//版本号
				merAccessRes.setMer_trace(StringUtils.trim(resInfo.getMerTrace()));//		商户流水号
				merAccessRes.setTrade_no(StringUtils.trim(resInfo.getTradeNo()));//  联动交易号
				merAccessRes.setAmount(StringUtils.trim(resInfo.getAmount()));//	支付金额
				merAccessRes.setSettle_amt(StringUtils.trim(resInfo.getSettleAmt()));//	结算金额
				merAccessRes.setMer_check_date(StringUtils.trim(resInfo.getMerCheckDate()));//	平台对账日期
				merAccessRes.setExpire_time(expireTime);;//	过期时间
				merAccessRes.setPay_type(StringUtils.trim(req.getPay_type()));//支付类型				
				merAccessRes.setOpenid(StringUtils.trim(resInfo.getOpenId()));//用户授权标识
				merAccessRes.setAppid(StringUtils.trim(resInfo.getAppId()));//微信及支付宝的 appId
				merAccessRes.setPrepay_id(StringUtils.trim(resInfo.getPrepayId()));//预支付交易会话ID
				merAccessRes.setTime_stamp(StringUtils.trim(resInfo.getTimestamp()));//时间戳
				merAccessRes.setNonce_str(StringUtils.trim(resInfo.getNonceStr()));//随机字符串
				merAccessRes.setPay_sign_type(StringUtils.trim(resInfo.getPaySignType()));//签名算法
				merAccessRes.setPay_sign(StringUtils.trim(resInfo.getPaySign()));//支付签名
				//resMeta.setRet_msg("支付成功");//返回描述
				
				resMessage.setData(merAccessRes);
			    } 		
		 } else {//响应结果为空，支付超时
			resMeta.setRet_code(MerAccessEC.SYSTEM_ERR);
			resMeta.setRet_msg("系统忙");
		 }

		resMessage.setMeta(resMeta);
		resMessage.setLinks(null);
		return resMessage;
	}
	
	@Override
	public Map<String, Object> mposPay(Map<String, Object> req) {
		
		Map<String, Object> resMessage = new HashMap<String, Object>();
		try {
			//惠商验签
			HsplusUtil.verifySign(req);
			log.info(String.format("智能POS支付，请求参数为:%s", req));
			//参数校验
			checkRequestParameter(req);
			String funCode = StringUtils.trim(req.get(XmlData.FUNCODE)) ;
			String rpid = StringUtils.trim(req.get(XmlData.RPID)) ;          
			String reqDate = StringUtils.trim(req.get(XmlData.REQDATE)) ;         
			String reqTime = StringUtils.trim(req.get(XmlData.REQTIME)) ;          	
			String subMerId = StringUtils.trim(req.get(XmlData.SUBMERID)) ;          //	          惠商子商户号
			String shopNo = StringUtils.trim(req.get(XmlData.SHOPNO)) ;              //	           门店号
			String operatorId = StringUtils.trim(req.get(XmlData.OPERATORID));       //	            操作员ID
			String deviceType = StringUtils.trim(req.get(XmlData.DEVICETYPE)) ;      //	            设备类型	
			String proxyId = StringUtils.trim(req.get(XmlData.PROXYID)) ;            //		集成商Id
			String deviceInfo = StringUtils.trim(req.get(XmlData.DEVICEINFO));       //     设备号
			String bpid = StringUtils.trim(req.get(XmlData.BPID)) ;                  //     第三方平台BPID
			String outOrderId = StringUtils.trim(req.get(XmlData.OUTORDERID)) ;      //	             第三方平台订单号       
			String userId = StringUtils.trim(req.get(XmlData.USERID)) ;              //	            第三方平台会员ID
			String mobileId = StringUtils.trim(req.get(XmlData.MOBILEID)) ;          //	            手机号			
			String bProductId = PropertiesUtil.getBusiStrValue("bsp.bproductid", "") ; //		业务产品号
				
			//组织请求merBusi参数
			MPOSBusiReq mposBusiReq = new MPOSBusiReq();
			mposBusiReq.setRpid(rpid);//通信流水
			mposBusiReq.setReqDate(reqDate);//请求日期		
			mposBusiReq.setReqTime(reqTime);//请求时间
			mposBusiReq.setVersion(DataDict.VERSION);
			mposBusiReq.setSubMerId(subMerId);//惠商子商户号
			mposBusiReq.setShopNo(shopNo);//门店号
			mposBusiReq.setOperatorId(operatorId);//操作员ID
			mposBusiReq.setDeviceType(deviceType);//设备类型 
			mposBusiReq.setDeviceInfo(deviceInfo);//设备类号
			mposBusiReq.setProxyId(proxyId);//集成商Id
			mposBusiReq.setBpid(bpid);//第三方平台BPID
			mposBusiReq.setOutOrderId(outOrderId);//第三方平台订单号
			mposBusiReq.setUserId(userId);//第三方平台会员ID	
			mposBusiReq.setMobileId(mobileId);//手机号	
			mposBusiReq.setbProductId(bProductId); //	业务产品号
			//mposBusiReq.setpProductId(pProductId); //	支付产品号
			
			//请求交易模块merBusi
			log.info(String.format("智能POS支付，调用远程服务接口请求参数为:%s", mposBusiReq));
			Message<MPOSBusiRes> merBusiRes = this.consumeBusiServerFeignClient.mposPay(mposBusiReq);
			log.info(String.format("智能POS支付，调用远程服务接口获取结果为:%s", merBusiRes));

			// 组织接入模块响应结果		
			//判断业务响应结果，不为空
			if (merBusiRes != null) {
				Meta metaData = merBusiRes.getMeta();
				String retCode = StringUtils.trim(metaData.getRet_code());
				String retMsg = StringUtils.trim(metaData.getRet_msg());
				resMessage.put(XmlData.RETCODE, retCode);//返回码
				resMessage.put(XmlData.RETMSG, retMsg);//返回描述
				
				if(StringUtils.isEmpty(retCode)) {
					resMessage.put(XmlData.RETCODE, MerAccessEC.SYSTEM_ERR);//返回码
					resMessage.put(XmlData.RETMSG, "系统忙");//返回描述
					
				}else if(MerAccessEC.SUCCESS.equals(retCode)) {//成功	
					MPOSBusiRes resInfo = merBusiRes.getData();
					resMessage.put(XmlData.FUNCODE, funCode);//交易功能码
					resMessage.put(XmlData.RPID, rpid);//交易序列号
					resMessage.put(XmlData.REQDATE, reqDate);//请求日期
					resMessage.put(XmlData.REQTIME, reqTime);//请求时间
					resMessage.put(XmlData.BPID, bpid);//第三方平台BPID
					resMessage.put(XmlData.OUTORDERID, outOrderId);//订单号
					resMessage.put(XmlData.PARTNERORDERID, StringUtils.trim(resInfo.getPartnerOrderId()));//支付流水
					resMessage.put(XmlData.ORDERDATE, reqDate);//订单日期
					//resMessage.put(XmlData.ORDERINFO, StringUtils.trim(resInfo.getO));//订单数据
					resMessage.put(XmlData.AMOUNT, StringUtils.trim(resInfo.getAmount()));//应付金额
					//resMessage.put(XmlData.PAYTYPE, payType);//支付渠道
							
				    } 		
			 } else {//响应结果为空，支付超时
				 resMessage.put(XmlData.RETCODE, MerAccessEC.SYSTEM_ERR);//返回码
				 resMessage.put(XmlData.RETMSG, "系统忙");//返回描述
			 }
	        //加签
			HsplusUtil.sign(resMessage);
			
		} catch (BusinessException e) {
			resMessage.put(XmlData.RETCODE, e.getCode());//返回码
			resMessage.put(XmlData.RETMSG, e.getMessage());//返回描述
		} 

		return resMessage;
	}
	
	private  Map<String, String> mapTransfer(Map<String, Object> reqMap) {
		Map<String, String> retMap = new HashMap<String, String>();
		for(Map.Entry<String, Object> entry:reqMap.entrySet()){
			retMap.put(entry.getKey(),StringUtils.trim(entry.getValue()));
		}
		return retMap;
	}
	private void checkRequestParameter(Map<String, Object> reqMap) {
		log.info("请求参数校验开始");
		try {
			ReqParaCheckUtil.checkRequireAndRegular(mapTransfer(reqMap), "Regex.mpospay.rule");
		} catch (BusinessException e) {
			String errCode = e.getCode();
            String errMsg = e.getMessage();
            throw new BusinessException(errCode,errMsg);
		}
		log.info("请求参数校验通过");
		String deviceType = StringUtils.trim(reqMap.get("deviceType"));
		if(!(DataDict.DEVICE_TYPE_POS.equals(deviceType) || DataDict.DEVICE_TYPE_Other.equals(deviceType))) {
			throw new BusinessException(MerAccessEC.REQPARAM_ERR, String.format("不支持[%s]类型的设备", deviceType));
		}
	}

	/** 
	* @Description: 扫码支付-用户被扫
	* @param:描述1描述
	* @return：返回结果描述
	* @throws：异常描述
	* @version: v1.0.0
	* @author: lile
	* @date: 2018年8月8日 下午4:34:44 
	*/
	@Override
	public Message<PaymentCodePayAccessRes> paymentCodePay(PaymentCodePayAccessReq req) {

		log.info(String.format("【扫码支付-用户被扫】，请求参数为:%s", req));
		//参数校验
		String payType = StringUtils.trim(req.getPay_type()) ;                   //	             支付方式
		if(!(payType.equals(DataDict.WECHAT) || payType.equals(DataDict.ALIPAY) || payType.equals(DataDict.PAYTYPE_YL))) {
			throw new BusinessException(MerAccessEC.REQPARAM_ERR, "公众号支付请求参数[pay_type]格式不正确");
		}			
		if(DataDict.WECHAT.equals(payType)) {
			payType = "20";
		}else if(DataDict.ALIPAY.equals(payType)) {
			payType = "21";
        } else if (DataDict.PAYTYPE_YL.equals(payType)) {
		    payType = DataDict.PAYTYPE_TRANS_M1;
        }
		String bProductId = PropertiesUtil.getBusiStrValue("bsp.bproductid", "") ; //业务产品号
		String pProductId="";
		if("20".equals(payType)) {
			pProductId = PropertiesUtil.getBusiStrValue("bsp.paymentCodePay.wechat.pproductid", "") ; //微信刷卡支付产品号
		}else if("21".equals(payType)) {
			pProductId = PropertiesUtil.getBusiStrValue("bsp.paymentCodePay.alipay.pproductid", "") ; //支付宝条码支付产品号			
        } else if (DataDict.PAYTYPE_TRANS_M1.equals(payType)) {
            pProductId = PropertiesUtil.getBusiStrValue("bsp.unionPayQR.pull.base.pproductid", "") ; //银联二维码被扫-基础产品号
        }
			
		//组织请求merBusi参数
		PaymentCodePayBusiReq paymentCodePayBusiReq = new PaymentCodePayBusiReq();
		paymentCodePayBusiReq.setRpid(RpidUtils.getRpid());//通信流水
		paymentCodePayBusiReq.setReqDate(TimeUtil.date8());//请求日期
		paymentCodePayBusiReq.setReqTime(TimeUtil.time6());//请求时间
		paymentCodePayBusiReq.setMerId(req.getMer_id()); //商户流水号
		paymentCodePayBusiReq.setNotifyUrl(req.getNotify_url());
		paymentCodePayBusiReq.setRetUrl(req.getRet_url());
		paymentCodePayBusiReq.setVersion(req.getVersion()); //	版本号
		paymentCodePayBusiReq.setMerTrace(req.getMer_trace());
		paymentCodePayBusiReq.setPayType(req.getPay_type());
		paymentCodePayBusiReq.setAuthCode(req.getAuth_code());
		paymentCodePayBusiReq.setAmount(req.getAmount());
		paymentCodePayBusiReq.setSubject(req.getSubject());
		paymentCodePayBusiReq.setRemark(req.getRemark());
		paymentCodePayBusiReq.setGoodsId(req.getGoods_id());
		paymentCodePayBusiReq.setGoodsInf(req.getGoods_inf());
		paymentCodePayBusiReq.setStoreId(req.getStore_id());
		paymentCodePayBusiReq.setOperatorId(req.getOperator_id());
		paymentCodePayBusiReq.setDiscountableAmount(req.getDiscountable_amount());
		paymentCodePayBusiReq.setUndiscountableAmount(req.getUndiscountable_amount());
		paymentCodePayBusiReq.setTradeNo(req.getTrade_no());
		paymentCodePayBusiReq.setbProductId(bProductId);
		paymentCodePayBusiReq.setpProductId(pProductId);
		paymentCodePayBusiReq.setPayType(payType);
		paymentCodePayBusiReq.setAccessType(req.getAccessType());
		paymentCodePayBusiReq.setAccessName(req.getAccessName());
		paymentCodePayBusiReq.setDepBankMerId(req.getDepBankMerId());
		
		//请求交易模块merBusi
		log.info(String.format("【扫码支付-用户被扫】调用【merusi】请求参数为:%s", paymentCodePayBusiReq));
		Message<PaymentCodePayBusiRes> merBusiRes = this.consumeBusiServerFeignClient.paymentCodePay(paymentCodePayBusiReq);
		log.info(String.format("【扫码支付-用户被扫】调用【merBusi】获取结果为:%s", merBusiRes));

		// 组织接入模块响应结果
		Message<PaymentCodePayAccessRes> resMessage = new Message<PaymentCodePayAccessRes>();
		Meta resMeta = new Meta();
		//判断业务响应结果，不为空
		if (merBusiRes != null) {
			Meta metaData = merBusiRes.getMeta();
			String retCode = StringUtils.trim(metaData.getRet_code());
			String retMsg = StringUtils.trim(metaData.getRet_msg());
			resMeta.setRet_code(retCode);//返回码
			resMeta.setRet_msg(retMsg);//返回描述
			
			if(StringUtils.isEmpty(retCode)) {
				resMeta.setRet_code(MerAccessEC.SYSTEM_ERR);
				resMeta.setRet_msg("系统忙");
				
			}else if(MerAccessEC.SUCCESS.equals(retCode)) {//成功				
				PaymentCodePayBusiRes paymentCodePayBusiRes = merBusiRes.getData();
				PaymentCodePayAccessRes merAccessRes = new PaymentCodePayAccessRes();
				merAccessRes.setMer_id(paymentCodePayBusiRes.getMerId());//商户号
				merAccessRes.setVersion(DataDict.VERSION);//版本号
				merAccessRes.setMer_trace(StringUtils.trim(paymentCodePayBusiRes.getMerTrace()));//		商户流水号
				merAccessRes.setTrade_no(StringUtils.trim(paymentCodePayBusiRes.getTradeNo()));//  联动交易号
				merAccessRes.setAmount(StringUtils.trim(paymentCodePayBusiRes.getAmount()));//	支付金额
				merAccessRes.setSettle_amt(StringUtils.trim(paymentCodePayBusiRes.getSettleAmt()));//	结算金额
				merAccessRes.setMer_check_date(StringUtils.trim(paymentCodePayBusiRes.getMerCheckDate()));//	平台对账日期
//				merAccessRes.setPay_type(StringUtils.trim(req.getPay_type()));//支付类型				述
				merAccessRes.setOrder_id(StringUtils.trim(paymentCodePayBusiRes.getOrderId()));
				merAccessRes.setMer_date(StringUtils.trim(paymentCodePayBusiRes.getOrderDate()));
				resMessage.setData(merAccessRes);
			    } 		
		 } else {//响应结果为空，支付超时
			resMeta.setRet_code(MerAccessEC.SYSTEM_ERR);
			resMeta.setRet_msg("系统忙");
		 }

		resMessage.setMeta(resMeta);
		resMessage.setLinks(null);
		return resMessage;
	}

	/** 
	* @Description: APP支付申请
	* @version: v1.0.0
	* @author: lile
	* @date: 2018年8月30日 下午4:54:12 
	*/
	@Override
	public Message<AppPayOrderAccessRes> appPayOrder(AppPayOrderAccessReq appPayReqAccessReq) {
		log.info(String.format("【APP支付-后台下单】，请求参数为:%s", appPayReqAccessReq));
		if(StringUtils.isNotEmpty(StringUtils.trim(appPayReqAccessReq.getGoods_id()))&&!StringUtils.trim(appPayReqAccessReq.getGoods_id()).matches(Regex.getRegexRuleByKey("regex.goods_id_umf"))){
			log.info("请求的参数[goods_id]格式或值不正确");
			throw new BusinessException(MerAccessEC.REQPARAM_ERR, "请求的参数[goods_id]格式或值不正确");
		}
		String bProductId = PropertiesUtil.getBusiStrValue("bsp.bproductid", "") ; //业务产品号
		String pProductId= PropertiesUtil.getBusiStrValue("bsp.appPay.base.pProductId", "");
		AppPayOrderBusiReq reqBusi = new AppPayOrderBusiReq();
		reqBusi.setRpid(RpidUtils.getRpid());//通信流水
		reqBusi.setReqDate(TimeUtil.date8());//请求日期
		reqBusi.setReqTime(TimeUtil.time6());//请求时间
		reqBusi.setMerId(appPayReqAccessReq.getMer_id()); //商户流水号
		reqBusi.setNotifyUrl(appPayReqAccessReq.getNotify_url());
		reqBusi.setVersion(appPayReqAccessReq.getVersion()); //	版本号
		reqBusi.setMerTrace(appPayReqAccessReq.getMer_trace());
		reqBusi.setAmount(appPayReqAccessReq.getAmount());
		reqBusi.setRemark(appPayReqAccessReq.getRemark());
		reqBusi.setGoodsId(appPayReqAccessReq.getGoods_id());
		reqBusi.setGoodsInf(appPayReqAccessReq.getGoods_inf());
		reqBusi.setTradeNo(appPayReqAccessReq.getTrade_no());
		reqBusi.setbProductId(bProductId);
		reqBusi.setpProductId(pProductId);
		reqBusi.setUserIp(appPayReqAccessReq.getUser_ip());
		reqBusi.setMerCustId(appPayReqAccessReq.getMer_cust_id());
		reqBusi.setExpireTime(appPayReqAccessReq.getExpire_time());
		reqBusi.setAccessName(appPayReqAccessReq.getAccessName());
		reqBusi.setAccessType(appPayReqAccessReq.getAccessType());
		
		//请求交易模块merBusi
		log.info(String.format("【APP支付-后台下单】调用【merusi】请求参数为:%s", reqBusi));
		Message<AppPayOrderBusiRes> busiRes = this.consumeBusiServerFeignClient.appPayBackEndOrder(reqBusi);
		log.info(String.format("【APP支付-后台下单】调用【merBusi】获取结果为:%s", busiRes));
		Meta resMeta = new Meta();
		Message<AppPayOrderAccessRes> resMessage = new Message<AppPayOrderAccessRes>();
		if (busiRes != null) {
			Meta metaData = busiRes.getMeta();
			String retCode = StringUtils.trim(metaData.getRet_code());
			String retMsg = StringUtils.trim(metaData.getRet_msg());
			resMeta.setRet_code(retCode);//返回码
			resMeta.setRet_msg(retMsg);//返回描述
			
			if(StringUtils.isEmpty(retCode)) {
				resMeta.setRet_code(MerAccessEC.SYSTEM_ERR);
				resMeta.setRet_msg("系统忙");
				
			}else if(MerAccessEC.SUCCESS.equals(retCode)) {//成功				
				AppPayOrderBusiRes appPayOrderBusiRes = busiRes.getData();
				AppPayOrderAccessRes merAccessRes = new AppPayOrderAccessRes();
				merAccessRes.setMer_id(appPayOrderBusiRes.getMerId());//商户号
				merAccessRes.setVersion(DataDict.VERSION);//版本号
				merAccessRes.setMer_trace(StringUtils.trim(appPayOrderBusiRes.getMerTrace()));//		商户流水号
				merAccessRes.setTrade_no(StringUtils.trim(appPayOrderBusiRes.getTradeNo()));//  联动交易号
				merAccessRes.setAmount(StringUtils.trim(appPayOrderBusiRes.getAmount()));//	支付金额
//				merAccessRes.setPay_type(StringUtils.trim(req.getPay_type()));//支付类型				述
				merAccessRes.setOrder_id(StringUtils.trim(appPayOrderBusiRes.getOrderId()));
				merAccessRes.setMer_date(StringUtils.trim(appPayOrderBusiRes.getOrderDate()));
				merAccessRes.setPay_sign(appPayOrderBusiRes.getPaySign());
				merAccessRes.setPay_trade_no(appPayOrderBusiRes.getBankSeq());
				resMessage.setData(merAccessRes);
			    } 		
		 } else {//响应结果为空，支付超时
			resMeta.setRet_code(MerAccessEC.SYSTEM_ERR);
			resMeta.setRet_msg("系统忙");
		 }

		resMessage.setMeta(resMeta);
		resMessage.setLinks(null);
		return resMessage;
	}

}
